<?php
session_start();
header("content-type:text/html;charset=utf-8");
$iipp = $_SERVER["REMOTE_ADDR"];
if(!isset($_SESSION['userr'])||$_SESSION['userr']=="")

{
	?>
	<script type="text/javascript">
//	alert("好像还没有登录的样子！");
	</script>
	 <?php
	// echo "您没有登录";
	echo $_SESSION['userr'];
	header("refresh:0;url=index.html");
}
else
{

//if($_COOKIE['key']==$_SESSION['key'] ){
    if($iipp==$iipp){




include "../config/conn.php"; 
if ($_SERVER["REQUEST_METHOD"] == "POST") {

if ($_POST["yxurl"]){

    if (isset($_FILES['yximg']) && $_FILES['yximg']['error'] == 0) {
        // 获取文件信息
        $fileTmpPath = $_FILES['yximg']['tmp_name'];
        $fileName = $_FILES['yximg']['name'];
        $fileSize = $_FILES['yximg']['size'];
        $fileType = $_FILES['yximg']['type'];

        // 设置文件保存目录
        $uploadDir = '../data/img/';
        // 确保上传目录存在
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }

        // 生成唯一的文件名，防止重复
        $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
        $newFileName = uniqid('game_') . '.' . $fileExtension;

        // 设置目标文件路径
        $uploadFilePath = $uploadDir . $newFileName;
    }
   if (move_uploaded_file($fileTmpPath, $uploadFilePath)) {

       $ok = '上传成功！';
   }
$yxurl=$_POST["yxurl"];
$yxname=$_POST["yxname"];
$yximg=$uploadFilePath;
$yxms=$_POST["yxms"];
$yxcny=$_POST["yxcny"];
$yxvip=$_POST["yxvip"];
$zt=$_POST["zt"];

$sql2 = "INSERT INTO game(name,ms,url,img,vip,cny,zt) VALUES('$yxname','$yxms','$yxurl','$yximg','$yxvip','$yxcny','$zt')";
$obj2 = mysqli_query($conn,$sql2);


echo "<script>alert('添加成功');location='game_add.php';</script>";
$_POST = array();
}
}
?>

<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>添加游戏</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
          
    <body>
    <div class="x-body">
        <form method="post" action="#" enctype="multipart/form-data">

          <div class="layui-form-item">
              <label for="username" class="layui-form-label">
                  <span class="x-red">*</span>游戏地址url
              </label>
              <div class="layui-input-block">
                  <textarea placeholder="请输入内容" id="desc" name="yxurl" class="layui-textarea"></textarea>
              </div>
          </div>
          
          
          
 <div class="layui-form-item layui-form-text">
              <label for="desc" class="layui-form-label">
                  游戏名称
              </label>
              <div class="layui-input-block">
                  <textarea placeholder="请输入内容" id="desc" name="yxname" class="layui-textarea"></textarea>
              </div>
          </div>
          

          <div class="layui-form-item">
              <label for="username" class="layui-form-label">
                  <span class="x-red">*</span>游戏描述
              </label>
              <div class="layui-input-block">
                  <textarea placeholder="请输入内容" id="desc" name="yxms" class="layui-textarea"></textarea>
              </div>
          </div>




          <div class="layui-form-item layui-form-text">
              <label for="desc" class="layui-form-label">
                 游戏价格
              </label>
              <div class="layui-input-block">
                  <textarea placeholder="请输入内容" id="desc" name="yxcny" class="layui-textarea"></textarea>
              </div>
          </div>
          
                              <div class="layui-form-item">
              <label for="username" class="layui-form-label">
                  <span class="x-red">*</span>会员
              </label>
              <div class="layui-input-inline">
                  <select name="yxvip">
                      <option value="6">收费游戏</option>
                    <option value="1">免注册畅玩</option>
                     <option value="2">注册会员</option>
                    <option value="3">青铜会员</option>
                    <option value="4">铂金会员</option>
                    <option value="5">钻石会员</option>
                  </select>
              </div>
          </div>
          
          
                              <div class="layui-form-item">
              <label for="yxzt" class="layui-form-label">
                  <span class="x-red">*</span>状态
              </label>
              <div class="layui-input-inline">
                  <select name="zt">
                    <option value="1">显示</option>
                    <option value="0">下架</option>
                  </select>
              </div>
          </div>
          
            <div class="layui-form-item">
      <label for="username" class="layui-form-label">
          <span class="x-red">*</span>游戏图片
      </label>
      <div class="layui-input-block">
          <input type="file" id="yximg" name="yximg" class="layui-input">
          <div id="img_preview" style="margin-top: 10px;">
              <!-- 图片预览区域 -->
          </div>
      </div>
  </div>
          
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">
                  增加游戏
              </button>
          </div>
      </form>
    </div>
    
    <script>
    // 图片预览功能
    document.getElementById('yximg').addEventListener('change', function(event) {
        var reader = new FileReader();
        reader.onload = function(e) {
            var imgElement = document.createElement('img');
            imgElement.src = e.target.result;
            imgElement.style.maxWidth = '200px'; // 限制最大宽度
            imgElement.style.maxHeight = '200px'; // 限制最大高度
            var previewArea = document.getElementById('img_preview');
            previewArea.innerHTML = ''; // 清除以前的预览
            previewArea.appendChild(imgElement);
        };
        reader.readAsDataURL(event.target.files[0]);
    });
</script>
    
  </body>

</html>

<?php
}else{echo header("refresh:0;url=exit.php");}}
?>